import { api } from '@/utils/request'

// 定义统计数据的类型
export interface OverallStatistics {
  totalOrders: number
  pendingOrders: number
  paidOrders: number
  cancelledOrders: number
  refundedOrders: number
  totalTickets: number
  validTickets: number
  usedTickets: number
  cancelledTickets: number
  totalUsers: number
  adminUsers: number
  normalUsers: number
  totalTrains: number
  activeTrains: number
  cancelledTrains: number
  totalSeats: number
  availableSeats: number
  occupiedSeats: number
}

export interface TodayStatistics {
  todayOrders: number
  todayTickets: number
  todayUsers: number
}

export interface RevenueStatistics {
  totalRevenue: number
  pendingRevenue: number
  refundedAmount: number
}

export interface PopularTrain {
  trainId: number
  trainNumber: string
  departureStation: string
  arrivalStation: string
  orderCount: number
}

export interface PopularRoute {
  departureStation: string
  arrivalStation: string
  ticketCount: number
}

export interface UserOrderStats {
  totalOrders: number
  paidOrders: number
  pendingOrders: number
  cancelledOrders: number
  totalSpent: number
}

export interface TrainSeatAvailability {
  trainId: number
  totalSeats: number
  availableSeats: number
  occupiedSeats: number
  occupancyRate: number
}

export interface ApiResponseData<T> {
  success: boolean
  message: string
  data: T
}

// 统计相关API
export const statisticsApi = {
  /**
   * 获取总体统计信息
   */
  getOverallStatistics(): Promise<ApiResponseData<OverallStatistics>> {
    return api.get('/statistics/overall')
  },

  /**
   * 获取今日统计
   */
  getTodayStatistics(): Promise<ApiResponseData<TodayStatistics>> {
    return api.get('/statistics/today')
  },

  /**
   * 获取收入统计
   */
  getRevenueStatistics(): Promise<ApiResponseData<RevenueStatistics>> {
    return api.get('/statistics/revenue')
  },

  /**
   * 获取热门车次统计
   * @param limit 限制返回数量，默认10
   */
  getPopularTrains(limit = 10): Promise<ApiResponseData<PopularTrain[]>> {
    return api.get(`/statistics/popular-trains?limit=${limit}`)
  },

  /**
   * 获取热门线路统计
   * @param limit 限制返回数量，默认10
   */
  getPopularRoutes(limit = 10): Promise<ApiResponseData<PopularRoute[]>> {
    return api.get(`/statistics/popular-routes?limit=${limit}`)
  },

  /**
   * 获取用户订单统计
   * @param userId 用户ID
   */
  getUserOrderStatistics(userId: number): Promise<ApiResponseData<UserOrderStats>> {
    return api.get(`/statistics/user/${userId}`)
  },

  /**
   * 获取车次座位可用性统计
   * @param trainId 车次ID
   */
  getTrainSeatAvailability(trainId: number): Promise<ApiResponseData<TrainSeatAvailability>> {
    return api.get(`/statistics/train/${trainId}/availability`)
  }
}

export default statisticsApi